package com.jorado.etl.service;

import com.jorado.basedata.BaseDataUtils;
import com.jorado.etl.model.TagInput;
import com.jorado.etl.model.TagResult;
import com.jorado.fasttext.FastTexter;
import com.jorado.fasttext.FilterManager;
import com.jorado.fasttext.TrainerProxy;
import com.jorado.lexicon.model.NatureUnit;
import com.jorado.lexicon.postag.PosTagger;
import com.jorado.search.core.util.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * fasttext打标签
 */
public class FastTextTagService {

    private String trainFile = "d:/fasttext/train.txt";
    private String modelFile = "d:/fasttext/model";
    private FastTexter fastTexter;
    private FilterManager filterManager;

    public FastTextTagService() {
        this.filterManager = new FilterManager();
        TrainerProxy trainerProxy = new TrainerProxy(trainFile, modelFile);
        this.fastTexter = trainerProxy.train();
    }

    public TagResult doTag(TagInput input, int limit) {

        TagResult tagResult = new TagResult();

        tagResult.setJobType(input.getJobType());
        tagResult.setJobTitle(input.getJobTitle());
        tagResult.setJobDescription(StringUtils.removeHTMLTag(input.getJobDescription()));

        StringBuilder sb = new StringBuilder();

        String jobTitle = filter(input.getJobTitle());
        String jobDescription = filter(input.getJobDescription());

        sb.append(jobTitle).append(" ");
        sb.append(segment(jobTitle)).append(" ");
        sb.append(segment(StringUtils.getChinese(jobDescription))).append(" ");

        String line = sb.toString();

        String[] words = StringUtils.splitString(line, " ", true);

        List<Map.Entry<String, Float>> labels = fastTexter.predict(limit, words);

        List<String> label = new ArrayList<>();
        List<String> labelName = new ArrayList<>();
        List<String> label1 = new ArrayList<>();
        List<String> labelName1 = new ArrayList<>();

        int i = 0;
        for (Map.Entry<String, Float> entry : labels) {

            i++;

            String jobTypeName = BaseDataUtils.getSubJobTypeName(entry.getKey());
            String bigJobType = BaseDataUtils.getParentTypeCode(entry.getKey());
            String bigJobTypeName = BaseDataUtils.getParentTypeName(entry.getKey());
            String inputBigJobType = BaseDataUtils.getParentTypeCode(input.getJobTypeId());
            String inputBigJobTypeName = BaseDataUtils.getParentTypeName(input.getJobTypeId());

            label.add(entry.getKey());
            labelName.add(jobTypeName);
            label1.add(bigJobType);
            labelName1.add(bigJobTypeName);

//            if (entry.getKey().equals(input.getJobTypeId()) || jobTypeName.equals(input.getJobType()) || input.getJobTitle().contains(jobTypeName)) {
//                if (tagResult.getHit() < 1) {
//                    tagResult.setHit(i);
//                }
//            }
//
//
//            if (tagResult.getHit() < 1) {
//            String[] jobTypeNames = jobTypeName.split("/");
//                for (String s : jobTypeNames) {
//                    if (input.getJobTitle().contains(s)) {
//                        tagResult.setHit(i);
//                        break;
//                    }
//
//                }
//            }
//
//
//            if (bigJobType.equals(inputBigJobType) || bigJobTypeName.equals(inputBigJobTypeName) || input.getJobTitle().contains(bigJobTypeName)) {
//                if (tagResult.getHit1() < 1) {
//                    tagResult.setHit1(i);
//                }
//            }
//
//            if (tagResult.getHit1() < 1) {
//            jobTypeNames = bigJobTypeName.split("/");
//                for (String s : jobTypeNames) {
//                    if (input.getJobTitle().contains(s)) {
//                        tagResult.setHit1(i);
//                        break;
//                    }
//
//                }
//            }
        }

        tagResult.setLabel(label);
        tagResult.setLabelName(labelName);

        tagResult.setLabel1(label1);
        tagResult.setLabelName1(labelName1);

        return tagResult;

    }

    /**
     * 过滤
     *
     * @param input
     * @return
     */
    public String filter(String input) {
        return filterManager.filter(input);
    }

    /**
     * 分词
     *
     * @param input
     * @return
     */
    public String segment(String input) {
        List<NatureUnit> natureUnits = PosTagger.getDefault().recognition(input);
        StringBuilder sb = new StringBuilder();
        for (NatureUnit natureUnit : natureUnits) {
            String word = natureUnit.getWord();
            //剔除单个字
            if (word.length() > 1) {
                sb.append(natureUnit.getWord());
                sb.append(" ");
            }
        }
        return sb.toString();
    }

}
